package com.linjing.trade.controller;

import com.linjing.item.model.ItemBo;
import com.linjing.trade.config.TradeConfig;
import com.linjing.trade.model.TradeOrderBo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author 仇冬林
 * @date 2022/5/7
 */

@Slf4j
@RestController
@RequestMapping("/trade")
@RequiredArgsConstructor
public class TradeController {
    private final RestTemplate restTemplate;
    private final TradeConfig tradeConfig;
//    @Reference
//    private ItemService itemService;


    @PostMapping("/createOrder")
    public String createOrder(@RequestBody TradeOrderBo order) {
        ItemBo item = restTemplate.getForObject("http://service-item/item/get/" + order.getItem().getId(), ItemBo.class);
        order.setItem(item);
        log.info("order: {}", order);
        return "ok";
    }

    @RequestMapping("/test")
    public String test() {
        return restTemplate.getForObject("http://service-item/item/load/", String.class);
    }


    @GetMapping("/config")
    public String testConfig(){
        return "username: " + tradeConfig.getUsername()  + ", password: " + tradeConfig.getPassword();
    }


//    @PostMapping("/test/dubbo/{id}")
//    public String testDubbo(@PathVariable("id") Long id){
//        ItemBo item = new ItemBo();
//        item.setId(id);
//        item.setItemCode("itemCode:" + id);
//        item.setItemName("itemName:" + id);
//        return itemService.addItem(item);
//    }
}
